# -*- coding: UTF-8 -*-

import os
import sys
curdir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(curdir)
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../Doctors")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../Helpers")))
from flask import Flask
from flask import request, jsonify
import DataDoctor
import SeasourceUtils

'''
https://juejin.im/post/5d5ce1875188255d465982ee
Flask中全局变量有current_app、request、g和session。不过需要注意的是虽然写着全局变量，但实际上这些变量都跟当前
请求的上下文环境有关，下面一起来看看。
current_app是当前激活程序的应用实例；request是请求对象，封装了客户端发出的HTTP请求中的内容；
g是处理请求时用作临时存储的对象，每次请求都会重设这个变量；session是用户会话，用于存储请求之间需要保存的值，它是一个字典。
'''
app = Flask(__name__)
g_doctor = None

@app.route("/")                                  # 指定多个URL
@app.route("/index",  methods=["GET", "POST"])   # 指定多个方法
def hello_world():
    return "Hello World!"

@app.route("/describe/<action>/<metadata_id>",  methods=["GET", "POST"])
def describe_seasource(action, metadata_id):
    global g_doctor
    return SeasourceUtils.describe_seasource(g_doctor, action, metadata_id)


@app.route("/analysis",  methods=["POST"])
def doctor_analysis():
    data = request.json        # 获取 JOSN 数据
    data = data.get('code')    # 以字典形式获取参数
    res = jsonify({"obj":res.to_json(orient = "records", force_ascii = False)})
    return res


@app.errorhandler(404)
def not_found_error(error):
    return make_response_error(404, error.description)


@app.errorhandler(500)
def internal_error(error):
    db.session.rollback()
    return make_response_error(500, error.description)


def setContext(doctor):
    global g_doctor
    g_doctor = doctor


if __name__ == "__main__":
    #app.run(host="0.0.0.0", port=8080, debug=True)
    app.run()

